package com.sz.virtualbank.web.controller.notification;

import com.sz.virtualbank.common.annotation.LoginRequired;
import com.sz.virtualbank.common.result.CommonResponse;
import com.sz.virtualbank.dao.entity.Notification;
import com.sz.virtualbank.dao.entity.User;
import com.sz.virtualbank.service.notification.NotificationService;
import com.sz.virtualbank.service.user.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 通知消息相关接口
 * 
 * @author Claude
 */
@RestController
@RequestMapping("/api/notification")
@Slf4j
@Api(tags = "通知消息接口")
public class NotificationController {

    @Autowired
    private NotificationService notificationService;
    
    @Autowired
    private UserService userService;
    
    /**
     * 查询消息列表
     */
    @GetMapping("/list")
    @ApiOperation("查询消息列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "type", value = "消息类型", dataType = "Integer", paramType = "query"),
        @ApiImplicitParam(name = "status", value = "消息状态", dataType = "Integer", paramType = "query"),
        @ApiImplicitParam(name = "page", value = "页码", dataType = "Integer", paramType = "query", required = true),
        @ApiImplicitParam(name = "size", value = "每页条数", dataType = "Integer", paramType = "query", required = true)
    })
    @LoginRequired
    public CommonResponse<Map<String, Object>> getNotificationList(
            @RequestParam(required = false) Integer type,
            @RequestParam(required = false) Integer status,
            @RequestParam Integer page,
            @RequestParam Integer size,
            HttpServletRequest request) {
        
        String token = request.getHeader("Authorization").replace("Bearer ", "");
        User user = userService.getCurrentUser(token);
        
        Map<String, Object> result = notificationService.getNotificationList(user.getId(), type, status, page, size);
        return CommonResponse.success(result);
    }
    
    /**
     * 查询消息详情
     */
    @GetMapping("/{id}")
    @ApiOperation("查询消息详情")
    @LoginRequired
    public CommonResponse<Notification> getNotificationDetail(@PathVariable Long id, 
                                                            HttpServletRequest request) {
        String token = request.getHeader("Authorization").replace("Bearer ", "");
        User user = userService.getCurrentUser(token);
        
        Notification notification = notificationService.getNotificationDetail(id, user.getId());
        return CommonResponse.success(notification);
    }
    
    /**
     * 标记消息为已读
     */
    @PutMapping("/read/{id}")
    @ApiOperation("标记消息为已读")
    @LoginRequired
    public CommonResponse<Boolean> markAsRead(@PathVariable Long id, 
                                            HttpServletRequest request) {
        String token = request.getHeader("Authorization").replace("Bearer ", "");
        User user = userService.getCurrentUser(token);
        
        boolean result = notificationService.markAsRead(id, user.getId());
        return CommonResponse.success(result);
    }
    
    /**
     * 标记所有消息为已读
     */
    @PutMapping("/read-all")
    @ApiOperation("标记所有消息为已读")
    @LoginRequired
    public CommonResponse<Boolean> markAllAsRead(HttpServletRequest request) {
        String token = request.getHeader("Authorization").replace("Bearer ", "");
        User user = userService.getCurrentUser(token);
        
        boolean result = notificationService.markAllAsRead(user.getId());
        return CommonResponse.success(result);
    }
    
    /**
     * 删除消息
     */
    @DeleteMapping("/{id}")
    @ApiOperation("删除消息")
    @LoginRequired
    public CommonResponse<Boolean> deleteNotification(@PathVariable Long id, 
                                                    HttpServletRequest request) {
        String token = request.getHeader("Authorization").replace("Bearer ", "");
        User user = userService.getCurrentUser(token);
        
        boolean result = notificationService.deleteNotification(id, user.getId());
        return CommonResponse.success(result);
    }
    
    /**
     * 查询未读消息数量
     */
    @GetMapping("/unread/count")
    @ApiOperation("查询未读消息数量")
    @LoginRequired
    public CommonResponse<Integer> countUnreadNotifications(HttpServletRequest request) {
        String token = request.getHeader("Authorization").replace("Bearer ", "");
        User user = userService.getCurrentUser(token);
        
        int count = notificationService.countUnreadNotifications(user.getId());
        return CommonResponse.success(count);
    }
}
